// Beginning Game Programming, Second Edition
// Chapter 10
// chrono_Game program source code file


#include "game.h"



//background image
LPDIRECT3DSURFACE9 back;

//sprite handler
LPD3DXSPRITE sprite_handler;

//ball sprite
LPDIRECT3DTEXTURE9 ball_image;
SPRITE ball;

//platform sprite
LPDIRECT3DTEXTURE9 platform_image[5];
SPRITE platform[5];


// chrono left walk 
LPDIRECT3DTEXTURE9 leftWalk_image;
SPRITE leftWalk;

////chrono atack
//LPDIRECT3DTEXTURE9 attack_image;
//SPRITE attack;


//chrono sprite
LPDIRECT3DTEXTURE9 chrono_image;
SPRITE chrono;



//the wave sound
CSound *sound_bounce;
CSound *sound_hit;
CSound *sound_swing;

//misc
long start = GetTickCount();
HRESULT result;

//---------------------------------------------------------------------------------------
//initializes the game
int Game_Init(HWND hwnd)
{
	//set random number seed
	srand(time(NULL));

	//initialize mouse
	if (!Init_Mouse(hwnd))
	{
		MessageBox(hwnd, "Error initializing the mouse", "Error", MB_OK);
		return 0;
	}

	//initialize keyboard
	if (!Init_Keyboard(hwnd))
	{
		MessageBox(hwnd, "Error initializing the keyboard", "Error", MB_OK);
		return 0;
	}

	//create sprite handler object
	result = D3DXCreateSprite(d3ddev, &sprite_handler);
	if (result != D3D_OK)
		return 0;

	//load the background image
	back = LoadSurface("namekRework.bmp", NULL);
	if (back == NULL)

		return 0;

	//load the chrono sprite
	chrono_image = LoadTexture("walk01.bmp", D3DCOLOR_XRGB(255,0,255));
	if (chrono_image == NULL)
		return 0;

	// load the texture run left 
	leftWalk_image = LoadTexture("walk02.bmp", D3DCOLOR_XRGB(255,0,255));
	if (leftWalk_image == NULL)
		return 0;

	//// load the texture run left 
	/*attack_image = LoadTexture("attack.bmp", D3DCOLOR_XRGB(255,0,255));
	if (attack_image == NULL)
		return 0;*/

	//load the platform sprite
	platform_image[1] = LoadTexture("Level 1Platform.bmp", D3DCOLOR_XRGB(255,0,255));
	if (platform_image[1] == NULL)
		return 0;

	//load the platform sprite
	platform_image[2] = LoadTexture("Level 1Platform.bmp", D3DCOLOR_XRGB(255,0,255));
	if (platform_image[2] == NULL)
		return 0;

	//set chrono properties
	chrono.x = 1; 
	chrono.y = 390;  /*SCREEN_HEIGHT*/
	chrono.width = 46;
	chrono.height = 64;
	chrono.curframe = 0;
	chrono.lastframe = 6;
	chrono.animdelay = .55;
	chrono.animcount = 0;
	chrono.movex = .5;
	chrono.movey = 0;


	//set attack properties
	//attack.x = 300; 
	//attack.y = 390;  /*SCREEN_HEIGHT*/
	//attack.width = 28;
	//attack.height = 44;
	//attack.curframe = 0;
	//attack.lastframe = 1;
	//attack.animdelay = .55;
	//attack.animcount = 0;
	//attack.movex = .5;
	//attack.movey = 0;

	
	//set platform properties
	platform[1].x = 84;
    platform[1].y =  358;
    platform[1].width = 143;
    platform[1].height = 18;

	//set platform 2 properties
	platform[2].x = 150;
    platform[2].y =  250;
    platform[2].width = 143;
    platform[2].height = 18;


	
	
	//load the hit wave file
    sound_swing = LoadSound("swing.wav");
    if (sound_swing == NULL)
        return 0;



	return 1;
}
//---------------------------------------------------------------------------------------
int Collision(SPRITE sprite1, SPRITE sprite2)
{
	RECT rect1;
	rect1.left = sprite1.x+1;
	rect1.top = sprite1.y+1;
	rect1.right = sprite1.x + sprite1.width-1;
	rect1.bottom = sprite1.y + sprite1.height-1;

	RECT rect2;
	rect2.left = sprite2.x+1;
	rect2.top = sprite2.y+1;
	rect2.right = sprite2.x + sprite2.width-1;
	rect2.bottom = sprite2.y + sprite2.height-1;

	RECT dest;
	return IntersectRect(&dest, &rect1, &rect2);
}
//---------------------------------------------------------------------------------------
//the main game loop
void Game_Run(HWND hwnd)
{
	
	static bool chronoR = false;
    float jumpLimit = 297;
	float jump = jumpLimit - 110;
	float walkV  = 390;
	static bool collided = false;
	//static bool Plat1Collide = false;
	





	////ball position vector
	//D3DXVECTOR3 position(0,0,0);

	//make sure the Direct3D device is valid
	if (d3ddev == NULL)
		return;

	//update mouse and keyboard
	Poll_Mouse();
	Poll_Keyboard();

	//after short delay, ready for next frame?	//this keeps the game running at a steady frame rate
	if (GetTickCount() - start >= 30)
	{
		//reset timing
		start = GetTickCount();

		//check for left arrow
		if (KEY_DOWN(VK_LEFT))
		{
			leftWalk_image = LoadTexture("walk02.bmp", D3DCOLOR_XRGB(255,0,255)); //loads the right-facing chrono texture
			if (++chrono.animcount > chrono.animdelay)	//loop to animate the sprites
			{
				//reset counter
				chrono.animcount = 0;

				//animate the sprite
				if (++chrono.curframe > chrono.lastframe)
					chrono.curframe = 0;
			}
			chrono.x -= 5;
		}
		////check for right arrow
		if (KEY_DOWN(VK_RIGHT))
		{
			
			leftWalk_image = LoadTexture("walk01.bmp", D3DCOLOR_XRGB(255,0,255)); //loads the left-facing chrono texture
			if (++chrono.animcount > chrono.animdelay)	//loop to animate the sprites
			{
		    chrono.x += 5;
			}	
		}
		//  
		//     

	}
	//   Poll_Mouse();

	////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//if(KEY_DOWN(VK_SPACE))
	//{
	//   attack_image = LoadTexture("attack.bmp", D3DCOLOR_XRGB(255,0,255)); //loads the attack-facing chrono texture
 //      if (++chrono.animcount > chrono.animdelay)	//loop to animate the sprites
	//		{
	//			//reset counter
	//			chrono.animcount = 1;

	//			//animate the sprite
	//			if (++chrono.curframe > chrono.lastframe)
	//				chrono.curframe = 0;
	//		}
	//		
	//}
	
	
	/*if (KEY_DOWN(VK_SPACE))
	{
<<<<<<< .mine
	  chrono.y = jumpLimit;
	  PlaySound(sound_swing);

=======
	   chrono.y = jumpLimit;*/
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	if (chrono.y == walkV) // checks to see if chrono is on ground level if = walkV collision is false
	{
		collided = false;
		
	}
       		
	  if (Collision(chrono,platform[1])) // checks the collision
		{
			
			collided = true; // collision true
			chrono.y = jumpLimit; // jumplimit = 390 on y axis
		}
		

	  if (Collision( chrono ,platform[1]) || chrono.y <= platform[1].y) // checks for collision on sprite if true will not be allowed to jump through platform
	  {
		  
		  //chrono.x = 110;
          chrono.y = 390; // 390 is ground level
	  }
	   
	
	if (collided) // colllision is TRUE
	{
		chrono.y = jumpLimit;
		
	}
	else if (!collided) // collision is NOT TRUE
	{

		chrono.y = walkV; // brnigs back down works as "gravity" kind of 
		
	}
	if (!Collision(chrono,platform[1])) // NOT colliding there for is on ground level
	{
		chrono.y = walkV; // ground level
	}

	if (KEY_DOWN(VK_UP) && collided == false) // JUMP
	{
	   chrono.y = jumpLimit; // chrono jumps to y axis 297
	   if (collided == true) // checks collision if TRUE 
	   {
		   chrono.y = walkV; // ground level
	   }
	   if (chrono.y == 358 && chrono.y == jumpLimit) // if chrono hits the edge of platform and chrono is at JUMP level chrono will fall
	   {
		   chrono.y = walkV; // bring chrono down 
	   }
	}
	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//secono paltform settings 


	/*if (Plat1Collide)
	{
		chrono.y = 297;
	}
	if(!Plat1Collide && chrono.y == 297)
	{
		collided = true;
	}*/

	if (chrono.y < 297)
	{

		if(KEY_DOWN(VK_UP) && chrono.y == 297)
		{
			chrono.y = jump;
		}
	}

	 

	

	

	
	


	
	/*if(KEY_DOWN(VK_SPACE))
	{
		
		if (Collision(chrono,platform))
		{
			MessageBox(NULL,"There is collision","Hello",MB_OK);
			chrono.y = jumpLimit;
			chrono.y = 350;
			chrono.x = 50;
			collided = true;
		}
		else
		{
			MessageBox(NULL,"There is NO collision","Hello",MB_OK);
		}
	}*/
	if (KEY_DOWN(VK_RIGHT))
	{
		//move the sprite
		chrono.x += chrono.movex;
		chrono.y += chrono.movey;
		


		////"warp" the sprite at screen edges
		/*if (chrono.x > SCREEN_WIDTH - chrono.width)
			chrono.x = 0;
		if (chrono.x < 0)
			chrono.x = SCREEN_WIDTH - chrono.width;*/

		//has animation delay reached threshold?
		if (++chrono.animcount > chrono.animdelay)
		{
			//reset counter
			chrono.animcount = 0;

			//animate the sprite
			if (++chrono.curframe > chrono.lastframe)
				chrono.curframe = 1;

		}
	}


	//configure the rect for the source tile

	////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//start rendering
	if (d3ddev->BeginScene())
	{
		//erase the entire background
		d3ddev->StretchRect(back, NULL, backbuffer, NULL, D3DTEXF_NONE);

		//start sprite handler
		sprite_handler->Begin(D3DXSPRITE_ALPHABLEND);


		//Vector for sprite 
		D3DXVECTOR3 position((float)chrono.x,(float)chrono.y, 0);
		D3DXVECTOR3 position1((float)platform[1].x, (float)platform[1].y,0);	
		D3DXVECTOR3 position2((float)platform[2].x, (float)platform[2].y,0);

		

		//configure the rect for the source tile
		RECT srcRect;
		int columns = 8;
		srcRect.left = (chrono.curframe % columns) * chrono.width;
		srcRect.top = (chrono.curframe / columns) * chrono.height;
		srcRect.right = srcRect.left + chrono.width;
		srcRect.bottom = srcRect.top + chrono.height;

		//draw the chrono

		if(chronoR)
		{
		position.x = (float)chrono.x;
		position.y = (float)chrono.y;
		sprite_handler->Draw(
			chrono_image,
			&srcRect,
			NULL,
			&position,
			D3DCOLOR_XRGB(255,255,255));
		}
		else
		{
			sprite_handler->Draw(
				leftWalk_image, 
				&srcRect,
				NULL,
				&position,
				D3DCOLOR_XRGB(255,255,255));
		}
			
		
		sprite_handler->Draw(
			platform_image[1],
			NULL,
			NULL,
			&position1,
			D3DCOLOR_XRGB(255,255,255));

		sprite_handler->Draw(
			platform_image[2],
			NULL,
			NULL,
			&position2,
			D3DCOLOR_XRGB(255,255,255));

		//stop drawing
		sprite_handler->End();

		//stop rendering
		d3ddev->EndScene();
	}

	//display the back buffer on the screen
	d3ddev->Present(NULL, NULL, NULL, NULL);

	//check for mouse button (to exit program)
	if (Mouse_Button(0))
	{
		PostMessage(hwnd, WM_DESTROY, 0, 0);
	}

	//check for escape key (to exit program)
	if (Key_Down(DIK_ESCAPE))
		PostMessage(hwnd, WM_DESTROY, 0, 0);
}
//---------------------------------------------------------------------------------------
//frees memory and cleans up before the game ends
void Game_End(HWND hwnd)
{

	if (ball_image != NULL)
		ball_image->Release();

	if (chrono_image != NULL)
		chrono_image->Release();

	if (back != NULL)
		back->Release();

	if (sprite_handler != NULL)
		sprite_handler->Release();

}

//---------------------------------------------------------------------------------------